package com.sjf.weibos;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.content.Context;
import android.util.Log;

import com.sjf.weibos.pojo.RequestToken;
import com.sjf.weibos.protocol.ProtocolUtil;

public class OAuth {
	private static ExecutorService executorService=Executors.newFixedThreadPool(5);
	public static String getRequestToken(String url,RequestToken requestToken){
	try {
		return url+ProtocolUtil.copy(requestToken);
	} catch (IllegalArgumentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SecurityException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InstantiationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InvocationTargetException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchMethodException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
public static String SendMsg(final Context context,final String url,final Object object,final String access){
	String content=null;
	executorService.execute(new Runnable() {
		public void run() {
			try {
				NetTool.openUrl(context, url, (Map)object, null,access);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	});
	return content;
}

public static String SendMsg(final Context context,final String url,final Object object,final String access,final File f){
	String content=null;
	executorService.execute(new Runnable() {
		
		public void run() {
			// TODO Auto-generated method stub
			try {
				NetTool.openUrl(context, url, (Map)object, f,access);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	});
	return content;
}
public static HashMap<String, String> subString(String url){
	int start,end;
	start=url.indexOf("?");
	end=url.length();
	if(start==-1)
		start=url.indexOf("#");
	if(start!=-1)
	return getMap(url.substring(start+1, end));
	return getMap(url);
}
public static HashMap<String, String> getMap(String param){
	Log.e("URL_GETMAP", param);
	HashMap<String, String> map =new HashMap<String, String>();
	if(param.indexOf("&")!=-1){
		String[] ss=param.split("&");
		for(String s:ss){
			String[] aa=s.split("=");
			if(aa.length>1)
			map.put(aa[0], aa[1]);
		}
	}else{
			String[] aa=param.split("=");
			if(aa.length>1)
			map.put(aa[0], aa[1]);
	}
	return map;
}
}
